package com.ruoyi.wq.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.GiveType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqDynamic;
import com.ruoyi.wq.domain.WqDynamicGive;
import com.ruoyi.wq.domain.WqNoticeGive;
import com.ruoyi.wq.mapper.WqDynamicGiveMapper;
import com.ruoyi.wq.service.IWqDynamicGiveService;
import com.ruoyi.wq.service.IWqDynamicService;
import com.ruoyi.wq.service.IWqNoticeGiveService;

/**
 * 动态点赞Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-29
 */
@Service
public class WqDynamicGiveServiceImpl extends ServiceImpl<WqDynamicGiveMapper, WqDynamicGive> implements IWqDynamicGiveService {
   
	@Autowired
    private WqDynamicGiveMapper wqDynamicGiveMapper;
    
    @Autowired
    private ISysUserService sysUserService;
    
    @Autowired
    private IWqNoticeGiveService wqNoticeGiveService;
    
    @Autowired
    private IWqDynamicService wqDynamicService;

    /**
     * 查询动态点赞
     * 
     * @param id 动态点赞主键
     * @return 动态点赞
     */
    @Override
    public WqDynamicGive selectWqDynamicGiveById(String id)
    {
        return wqDynamicGiveMapper.selectWqDynamicGiveById(id);
    }

    /**
     * 查询动态点赞列表
     * 
     * @param wqDynamicGive 动态点赞
     * @return 动态点赞
     */
    @Override
    public List<WqDynamicGive> selectWqDynamicGiveList(WqDynamicGive wqDynamicGive)
    {
        return wqDynamicGiveMapper.selectWqDynamicGiveList(wqDynamicGive);
    }

    /**
     * 新增动态点赞
     * 
     * @param wqDynamicGive 动态点赞
     * @return 结果
     */
    @Override
    public Boolean insertWqDynamicGive(WqDynamicGive wqDynamicGive){
    	Boolean isGvie = false;
    	SysUser sysUser = sysUserService.getById(SecurityUtils.getUserId());
    	WqDynamic dynamic = wqDynamicService.getById(wqDynamicGive.getDynamicId());
    	if(sysUser.getUserId().equals(dynamic.getUserId())){
    		// 不能给自己点赞
    		return true;
    	}
    	WqDynamicGive give = getWqDynamicGive(wqDynamicGive.getDynamicId(), sysUser.getUserId());
    	if(give == null){
    		isGvie = true;
    		wqDynamicGive.setUserId(sysUser.getUserId());
        	wqDynamicGive.setAvatar(sysUser.getAvatar());
        	wqDynamicGive.setNickName(sysUser.getNickName());
        	wqDynamicGive.setPhone(sysUser.getPhonenumber());
        	wqDynamicGive.setSex(sysUser.getSex());
        	wqDynamicGiveMapper.insert(wqDynamicGive);
        	
        	// 点赞通知
        	WqNoticeGive noticeGive = new WqNoticeGive();
        	noticeGive.setDynamicId(dynamic.getId());
        	if(StringUtils.isNotBlank(dynamic.getImage())){
        		noticeGive.setDynamicImage(dynamic.getImage().split(",")[0]);
        	}
        	noticeGive.setGiveType(GiveType.DYNAMIC_GIVE.getCode());
        	
        	noticeGive.setUserId(sysUser.getUserId());
        	noticeGive.setAvatar(sysUser.getAvatar());
        	noticeGive.setNickName(sysUser.getNickName());
        	noticeGive.setPhone(sysUser.getPhonenumber());
        	noticeGive.setSex(sysUser.getSex());
        	noticeGive.setNoticeUserId(dynamic.getUserId());
        	wqNoticeGiveService.save(noticeGive);
    	}else{
    		isGvie = false;
    		wqDynamicGiveMapper.deleteById(give.getId());
    		wqNoticeGiveService.deleteByDynamicIdAndUserId(dynamic.getId(), sysUser.getUserId());
    	}
    	// 统计动态点赞数量
    	Integer giveNum = getGiveNumByDynamicId(wqDynamicGive.getDynamicId());
    	
    	// 更新动态点赞数量
    	dynamic.setGiveNum(giveNum);
    	wqDynamicService.updateById(dynamic);
    	
    	// 更新用户未读消息数量
    	SysUser user = sysUserService.getById(dynamic.getUserId());
    	Integer giveNewNum = wqNoticeGiveService.getUnReadNum(dynamic.getUserId());
    	user.setGiveNewNum(giveNewNum);
    	sysUserService.updateById(user);
    	return isGvie;
    }
    
    private Integer getGiveNumByDynamicId(String dynamicId){
    	QueryWrapper<WqDynamicGive> qw = new QueryWrapper<>();
    	WqDynamicGive entity = new WqDynamicGive();
    	entity.setDynamicId(dynamicId);
    	entity.setStatus("0");
    	qw.setEntity(entity);
    	return baseMapper.selectCount(qw);
    }
    
    private WqDynamicGive getWqDynamicGive(String dynamicId, Long userId){
    	QueryWrapper<WqDynamicGive> qw = new QueryWrapper<>();
    	WqDynamicGive entity = new WqDynamicGive();
    	entity.setDynamicId(dynamicId);
    	entity.setUserId(userId);
    	qw.setEntity(entity);
    	return baseMapper.selectOne(qw);
    }

    /**
     * 修改动态点赞
     * 
     * @param wqDynamicGive 动态点赞
     * @return 结果
     */
    @Override
    public int updateWqDynamicGive(WqDynamicGive wqDynamicGive)
    {
        wqDynamicGive.setUpdateTime(DateUtils.getNowDate());
        return wqDynamicGiveMapper.updateWqDynamicGive(wqDynamicGive);
    }

    /**
     * 批量删除动态点赞
     * 
     * @param ids 需要删除的动态点赞主键
     * @return 结果
     */
    @Override
    public int deleteWqDynamicGiveByIds(String[] ids)
    {
        return wqDynamicGiveMapper.deleteWqDynamicGiveByIds(ids);
    }

    /**
     * 删除动态点赞信息
     * 
     * @param id 动态点赞主键
     * @return 结果
     */
    @Override
    public int deleteWqDynamicGiveById(String id)
    {
        return wqDynamicGiveMapper.deleteWqDynamicGiveById(id);
    }
}
